﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Linq.Expressions;
using Newtonsoft.Json.Linq;

using CanDoo;
using CanDoo.Core;
using CanDoo.Data;
using CanDoo.Web;
using CanDoo.FineUI;
using CanDoo.Extensions;
using CanDoo.Oil.Core.Models;
using CanDoo.Oil.Core.Contracts;
using CanDoo.Admin.Core.Contracts;
using FineUIPro;

namespace CanDoo.Oil.WebApp.oilGetOilCode
{
    public partial class Edit : EditPagebase<IGetOilCodeContract, GetOilCode>
    {
        public ICustomerContract CustomerContract { get; set; }
        public IProductContract ProductContract { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //FineUIListHelper.BindDDL<Customer>(CustomerContract.GetResolvedTree(), 0L, ed_Customer,true);
                //FineUIListHelper.BindDDL(CustomerContract.EntityName, this.CustomerContract.GetSelectList(), 0L, ed_Customer,true);
                FineUIListHelper.BindDDL("油品",ProductContract.GetSelectListByOil(), 0L, ed_Oil, true);
                //FineUIListHelper.BindDDL(ProductContract.EntityName, this.ProductContract.GetSelectList(), 0L, ed_Product,true);
                if (PageMode != pagemode.Add)
                    LoadData();
            }
        }

        protected override void LoadData(GetOilCode entity)
        {
            ed_Name.SetValue(entity.Name);
            if (entity.Customer!=null)
            {
                ed_Customer.Values=new string[] { entity.Customer.Id.ToString() };
                ed_Customer.Text=entity.Customer.Name;
            }
            if (!entity.OilId.IsNull())
                ed_Oil.SetValue(entity.OilId);
            ed_Num.SetValue(entity.Num);
            ed_Price.SetValue(entity.Price);
            ed_Money.SetValue(entity.Money);
            ed_OilCode.SetValue(entity.OilCode);
            ed_Used.SetValue(entity.Used);
            ed_Lasted.SetValue(entity.Lasted);
        }

        protected override OperationResult SaveData(GetOilCode entity)
        {
            entity.Name = ed_Name.GetValue().ToString();
            if (ed_Customer.Values.Count() > 0 && ed_Customer.Values[0].ToLong() >  0L)
                entity.CustomerId = ed_Customer.Values[0].ToLong();
            else
                entity.CustomerId = 0L;
            if (ed_Oil.GetValue().ToLong() == 0L)
                entity.OilId = 0L;
            else
                entity.OilId = ed_Oil.GetValue().ToLong();
            entity.Num = ed_Num.GetValue().ToDouble();
            entity.Price = ed_Price.GetValue().ToDouble();
            entity.Money = ed_Money.GetValue().ToDouble();
            entity.OilCode = ed_OilCode.GetValue().ToString();
            entity.Used = ed_Used.GetValue().ToDouble();
            entity.Lasted = ed_Lasted.GetValue().ToDouble();
            return base.SaveData(entity);
            
        }
    }
}
